﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace TheVoid.View
{
    public class OptionManager : DrawableGameComponent
    {
        private GraphicsDeviceManager graphics;
        private SpriteBatch spriteBatch;
        private int selectedOption = 0;
        public int SelectionIndex { get { return selectedOption; } }
        private Color normalColor = Color.White;
        public Color NormalColor
        {
            get
            {
                return normalColor;
            }
            set
            {
                optionDrawInformations = null;
                normalColor = value;
            }
        }
        private Color highlightColor = Color.Silver;
        public Color HighlightColor
        {
            get
            {
                return highlightColor;
            }
            set
            {
                optionDrawInformations = null;
                highlightColor = value;
            }
        }
        private List<String> options = null;
        public List<String> Options
        {
            get
            {
                return options;
            }
            set
            {
                optionDrawInformations = null;
                selectedOption = 0;
                this.options = value;
            }
        }
        private List<DrawInformations> optionDrawInformations;
        private SpriteFont defaultFont;
        private Rectangle rect;
        public Rectangle Rect
        {
            get
            {
                return rect;
            }
            set
            {
                optionDrawInformations = null;
                rect = value;
            }
        }

        public OptionManager(Game game, GraphicsDeviceManager graphics) : base(game)
        {
            this.graphics = graphics;
            this.Options = new List<string>();
            this.Rect = new Rectangle();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            spriteBatch = new SpriteBatch(GraphicsDevice);
            defaultFont = Game.Content.Load<SpriteFont>("DefaultFont");
        }

        public override void Draw(GameTime gameTime)
        {
            if (this.optionDrawInformations == null)
            {
                calculateDrawInformations();
            }
            spriteBatch.Begin();
            foreach (DrawInformations infos in optionDrawInformations)
            {
                spriteBatch.DrawString(this.defaultFont, infos.Text, infos.Position, infos.Color);
            }
            spriteBatch.End();
        }

        private void calculateDrawInformations()
        {
            this.optionDrawInformations = new List<DrawInformations>();
            float height = this.rect.Y;
            for(int i = 0; i < options.Count; i++)
            {
                String option = options[i];
                Vector2 size = this.defaultFont.MeasureString(option);
                Color color = (i == selectedOption) ? highlightColor : normalColor;
                optionDrawInformations.Add(new DrawInformations() { Position = new Vector2(this.rect.X, height), Text = option, Color = color });
                height += size.Y;
            }
        }

        private class DrawInformations
        {
            public String Text { get; set; }
            public Vector2 Position { get; set; }
            public Color Color { get; set; }
        }

        public void SelectNextOption()
        {
            selectedOption = (selectedOption + 1) % this.options.Count;
            optionDrawInformations = null;
        }

        public void SelectPreviousOption()
        {
            selectedOption -= 1;
            if (selectedOption < 0)
            {
                selectedOption = options.Count - 1;
            }
            optionDrawInformations = null;
        }
    }
}
